home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / listwalk.cls < prev    next >
Text File  |  1997-06-14  |  3KB  |  120 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CListWalker"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. Public Enum EErrorListWalker
  13.     eeBaseListWalker = 13110    ' CListWalker
  14. End Enum
  15.  
  16. ' Implement Basic-friendly version of IEnumVARIANT
  17. Implements IVariantWalker
  18. ' Delegate to class that implements real IEnumVARIANT
  19. Private vars As CEnumVariant
  20.  
  21. ' Connect back to parent collection
  22. Private connect As CList
  23. ' Current link
  24. Private lnkCur As CLink
  25.  
  26. ' Return IEnumVARIANT (indirectly) to client collection
  27. Friend Function NewEnum() As stdole.IEnumVARIANT
  28.     Set NewEnum = vars
  29. End Function
  30.  
  31. ' Attach a list to iterator
  32. Sub Attach(connectA As CList, Optional fEnumerate As Boolean = False)
  33.     ' Initialize position in collection
  34.     Set connect = connectA
  35.     If fEnumerate Then
  36.         ' Connect walker to CEnumVariant so it can call methods
  37.         Set vars = New CEnumVariant
  38.         vars.Attach Me
  39.     End If
  40. End Sub
  41.  
  42. ' Report whether there are more links to iterate
  43. Function More() As Boolean
  44.     If lnkCur Is Nothing Then
  45.         ' Don't skip the first time through
  46.         Set lnkCur = connect.Head
  47.     Else
  48.         ' Skip to the next item
  49.         Set lnkCur = lnkCur.NextLink
  50.     End If
  51.     ' When the next link is nothing, we're done (handles empty list)
  52.     If Not lnkCur Is Nothing Then More = True
  53. End Function
  54.  
  55. ' Default member
  56. Property Get Item() As Variant
  57. Attribute Item.VB_UserMemId = 0
  58.     If IsObject(lnkCur.Item) Then
  59.         Set Item = lnkCur.Item
  60.     Else
  61.         Item = lnkCur.Item
  62.     End If
  63. End Property
  64.  
  65. ' Expose current link to friends
  66. Friend Property Get CurLink() As CLink
  67.     Set CurLink = lnkCur
  68. End Property
  69. Friend Property Set CurLink(lnkCurA As CLink)
  70.     Set lnkCur = lnkCurA
  71. End Property
  72.  
  73. ' Implement IVariantWalker methods
  74. Private Function IVariantWalker_More(v As Variant) As Boolean
  75.     ' Move to next element
  76.     IVariantWalker_More = More
  77.     If IVariantWalker_More = False Then Exit Function
  78.     ' Return element through reference
  79.     If IsObject(lnkCur.Item) Then
  80.         Set v = lnkCur.Item
  81.     Else
  82.         v = lnkCur.Item
  83.     End If
  84. End Function
  85.  
  86. Private Sub IVariantWalker_Reset()
  87.     ' Move to first element
  88.     If connect.Count Then Set lnkCur = connect.Head
  89. End Sub
  90.  
  91. Private Sub IVariantWalker_Skip(c As Long)
  92.     ' Skip a given number of elements
  93.     Dim i As Long, v As Variant
  94.     For i = 1 To c
  95.         If IVariantWalker_More(v) = False Then Exit For
  96.     Next
  97. End Sub
  98. '
  99.  
  100. #If fComponent = 0 Then
  101. Private Sub ErrRaise(e As Long)
  102.     Dim sText As String, sSource As String
  103.     If e > 1000 Then
  104.         sSource = App.ExeName & ".ListWalker"
  105.         Select Case e
  106.         Case eeBaseListWalker
  107.             BugAssert True
  108.        ' Case ee...
  109.        '     Add additional errors
  110.         End Select
  111.         Err.Raise COMError(e), sSource, sText
  112.     Else
  113.         ' Raise standard Visual Basic error
  114.         sSource = App.ExeName & ".VBError"
  115.         Err.Raise e, sSource
  116.     End If
  117. End Sub
  118. #End If
  119.  
  120.